package com.middleware.utils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class CommonUtil {

    public static boolean isWrapperType(Class<?> clazz) {
        return clazz == Integer.class || clazz == Long.class ||
                clazz == Double.class || clazz == Float.class ||
                clazz == Boolean.class || clazz == Character.class ||
                clazz == Byte.class || clazz == Short.class;
    }
    @SuppressWarnings("unchecked")
    public static <T> T getPrimitiveDefaultValue(Class<T> clazz) {
        if (clazz == int.class) return (T) Integer.valueOf(0);
        if (clazz == long.class) return (T) Long.valueOf(0L);
        if (clazz == boolean.class) return (T) Boolean.FALSE;
        if (clazz == double.class) return (T) Double.valueOf(0.0);
        if (clazz == float.class) return (T) Float.valueOf(0.0f);
        if (clazz == char.class) return (T) Character.valueOf('\0');
        if (clazz == byte.class) return (T) Byte.valueOf((byte)0);
        if (clazz == short.class) return (T) Short.valueOf((short)0);
        return null;
    }

    /**
     * 下划线转驼峰命名
     */
    public static String toCamelCase(String columnName) {
        String[] parts = columnName.split("_");
        StringBuilder camelCase = new StringBuilder(parts[0].toLowerCase());

        for (int i = 1; i < parts.length; i++) {
            camelCase.append(parts[i].substring(0, 1).toUpperCase())
                    .append(parts[i].substring(1).toLowerCase());
        }
        return camelCase.toString();
    }

    /**
     * 类型转换
     */
    public static Object convertType(Object value, Class<?> targetType) {
        if (value == null) return null;

        if (targetType.isInstance(value)) {
            return value;
        }

        // 简单的类型转换
        if (targetType == Integer.class || targetType == int.class) {
            return ((Number) value).intValue();
        } else if (targetType == Long.class || targetType == long.class) {
            return ((Number) value).longValue();
        } else if (targetType == String.class) {
            return value.toString();
        }else if(targetType == Double.class || targetType == double.class){
            return ((Number) value).doubleValue();
        }else if(targetType == Float.class || targetType == float.class){
            return ((Number) value).floatValue();
        }else if(targetType == Boolean.class || targetType == boolean.class){
            return Boolean.parseBoolean(value.toString());
        }else if(targetType == Date.class){
            return new Date(Long.parseLong(value.toString()));
        }else if(targetType == LocalDateTime.class){
            String dateTimeStr = value.toString();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss[.S]");
            // 处理可能的毫秒部分
            if (dateTimeStr.contains(".")) {
                return LocalDateTime.parse(dateTimeStr, formatter);
            } else {
                dateTimeStr = dateTimeStr.replace(' ', 'T');
                return LocalDateTime.parse(dateTimeStr);
            }
        }

        return value;
    }
}
